package com.example.purchase.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.purchase.MyApplication;
import com.example.purchase.R;
import com.example.purchase.bean.BaseBean;
import com.example.purchase.network.RetrofitClient;
import com.example.purchase.network.service.HttpService;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class Dialog_popWindow extends Dialog implements View.OnClickListener {

    private final Context context;
    private final int contentViewLayoutId;
    private TextView cancel_button,confirm_button,title,content;
    private EditText edit_username_text;
    private Boolean isOther;
    private Boolean flag = false;
    private String id;
    private final HttpService service = RetrofitClient.getInstance().getService(HttpService.class);

    public Dialog_popWindow(Context context, int themeResId , int contentViewLayoutId , Boolean isOther , String id) {
        super(context, themeResId);
        this.context = context;
        this.contentViewLayoutId = contentViewLayoutId;
        this.isOther = isOther;
        this.id = id;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (contentViewLayoutId){
            case MyApplication.EDIT_MESSAGE:     //修改弹窗
                setContentView(R.layout.dialog_edit_name);
                initCommon();
                initEditView();
                break;
            case MyApplication.DELETE_LIST:     //取消喜欢 删除订单弹窗
                setContentView(R.layout.dialog_isdelete);
                initCommon();
                initSidebarView();
                break;
        }
    }

    private void initCommon() {
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);

        cancel_button = findViewById(R.id.cancel_button);
        confirm_button = findViewById(R.id.confirm_button);
        cancel_button.setOnClickListener(this);
        confirm_button.setOnClickListener(this);
    }

    private void initEditView() {
        edit_username_text = findViewById(R.id.edit_username_text);
        flag = true;
    }

    private void  initSidebarView() {
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        if (isOther) title.setText("取消喜欢"); content.setText("确认移除喜欢列表");
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel_button:
                Toast.makeText(context, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.confirm_button:
                if (flag){
                    String new_name = String.valueOf(edit_username_text.getText());
                    if (new_name.isEmpty()){
                        Toast.makeText(context, "请输入新昵称", Toast.LENGTH_SHORT).show();
                        break;
                    }else {
                        Log.e("TAG", "onClick: " + new_name);
                        String token = context.getSharedPreferences("userMessage",Context.MODE_PRIVATE).getString("token","eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjMxLCJpYXQiOjE2NjI1MTA3MTl9.WzyBbwckMPlnCasU5ol1ksWLU3SQijxqDwYETSVqSH4");
                        Log.e("TAG", "onClick: " + token);
                        service.updateUserName(new_name,token)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .filter(new Predicate<BaseBean<String>>() {
                                    @Override
                                    public boolean test(BaseBean<String> stringBaseBean) throws Throwable {
                                        Log.e("TAG", "test: " + stringBaseBean.getCode());
                                        Log.e("TAG", "test: " + stringBaseBean.getMsg());
                                        return String.valueOf(stringBaseBean.getCode()).equals("200");
                                    }
                                }).subscribe(new Consumer<BaseBean<String>>() {
                            @SuppressLint("CommitPrefEdits")
                            @Override
                            public void accept(BaseBean<String> stringBaseBean) throws Throwable {
                                context.getSharedPreferences("userMessage",Context.MODE_PRIVATE).edit().putString("userName",new_name);
                            }
                        });
                    }
                }else {
                    service.deleteTicket(id,context.getSharedPreferences("userMessage",Context.MODE_PRIVATE).getString("token",""))
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .filter(new Predicate<BaseBean<String>>() {
                                @Override
                                public boolean test(BaseBean<String> stringBaseBean) throws Throwable {
                                    return String.valueOf(stringBaseBean.getCode()).equals("200");
                                }
                            }).subscribe(new Consumer<BaseBean<String>>() {
                        @Override
                        public void accept(BaseBean<String> stringBaseBean) throws Throwable {
                            Toast.makeText(context, "请下拉刷新数据", Toast.LENGTH_SHORT).show();
                            Log.e("TAG", "accept: " + stringBaseBean.getMsg() );
                        }
                    });
                }
                flag = false;
                dismiss();
                break;
        }
    }
}
